import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  ScopeContext,
} from '@fast-crud/fast-crud';

import { ref } from 'vue';

import { DefGenTableColumnApi } from '#/api/devOperation/developer/defGenTableColumn';
import { useMessage } from '#/hooks/web/useMessage';
import { $t } from '#/locales';
import {
  createdTimeColumn,
  deleteButton,
  indexColumn,
  transformQuery,
  YES_NO_CONSTANT_DICT,
} from '#/plugins/fast-crud/common';

export function createCrudOptions(
  props: CreateCrudOptionsProps,
): CreateCrudOptionsRet {
  const selectedRowKeys = ref<string[]>([]);

  const { createMessage } = useMessage();
  return {
    crudOptions: {
      request: {
        pageRequest: DefGenTableColumnApi.pageRequest,
        addRequest: DefGenTableColumnApi.addRequest,
        editRequest: DefGenTableColumnApi.editRequest,
        delRequest: DefGenTableColumnApi.delRequest,
        infoRequest: DefGenTableColumnApi.infoRequest,
        transformQuery: (query) => {
          const tq = transformQuery(query);

          tq.model.tableId = props.context.tableIdRef.value;
          return tq;
        },
      },
      actionbar: {
        buttons: {
          add: { show: false },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: DefGenTableColumnApi.remove,
          }),
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        buttons: {
          sync: {
            size: 'small',
            type: 'link',
            text: '同步',
            async click(context: ScopeContext) {
              try {
                await DefGenTableColumnApi.syncField(
                  context.row.tableId,
                  context.row.id,
                );
                createMessage.success('同步成功');
              } finally {
                props.crudExpose.doRefresh();
              }
            },
          },
          copy: { show: false },
        },
        fixed: 'right',
        width: 220,
      },
      columns: {
        ...indexColumn(props.crudExpose),
        tableId: {
          title: $t('devOperation.developer.defGenTableColumn.tableId'),
          type: 'text',
          column: { show: false },
          form: { show: false },
        },
        name: {
          title: $t('devOperation.developer.defGenTableColumn.name'),
          type: 'text',
          search: { show: true },
          column: { width: 120 },
          addForm: { component: { disabled: true } },
          editForm: { component: { disabled: true } },
        },
        type: {
          title: $t('devOperation.developer.defGenTableColumn.type'),
          type: 'text',
          column: { width: 120 },
          form: { component: { disabled: true } },
        },
        comment: {
          title: $t('devOperation.developer.defGenTableColumn.comment'),
          type: 'text',
          search: { show: true },
          column: { width: 120 },
        },
        swaggerComment: {
          title: $t('devOperation.developer.defGenTableColumn.swaggerComment'),
          type: 'text',
          column: { width: 120 },
        },
        javaType: {
          title: $t('devOperation.developer.defGenTableColumn.javaType'),
          type: 'text',
          form: {
            component: {
              name: 'a-auto-complete',
              options: [
                { value: 'String' },
                { value: 'Long' },
                { value: 'Integer' },
                { value: 'Boolean' },
                { value: 'Double' },
                { value: 'BigDecimal' },
                { value: 'LocalDateTime' },
                { value: 'LocalDate' },
                { value: 'LocalTime' },
              ],
            },
          },
          column: { width: 120 },
        },
        javaField: {
          title: $t('devOperation.developer.defGenTableColumn.javaField'),
          type: 'text',
          column: { width: 120 },
        },
        tsType: {
          title: $t('devOperation.developer.defGenTableColumn.tsType'),
          type: 'text',
          form: {
            component: {
              name: 'a-auto-complete',
              options: [
                { value: 'string' },
                { value: 'number' },
                { value: 'boolean' },
                { value: 'string[]' },
                { value: 'number[]' },
                { value: 'any' },
                { value: 'tup,le' },
                { value: 'enum' },
              ],
            },
          },
          column: { width: 120 },
        },
        size: {
          title: $t('devOperation.developer.defGenTableColumn.size'),
          type: 'number',
          column: { width: 120 },
        },
        digit: {
          title: $t('devOperation.developer.defGenTableColumn.digit'),
          type: 'number',
          column: { width: 120 },
        },
        isPk: {
          title: $t('devOperation.developer.defGenTableColumn.isPk'),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          column: { width: 120 },
        },
        isRequired: {
          title: $t('devOperation.developer.defGenTableColumn.isRequired'),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          column: { width: 120 },
        },
        isLogicDeleteField: {
          title: $t(
            'devOperation.developer.defGenTableColumn.isLogicDeleteField',
          ),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          column: { width: 120 },
        },
        isVersionField: {
          title: $t('devOperation.developer.defGenTableColumn.isVersionField'),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          column: { width: 120 },
        },
        isIncrement: {
          title: $t('devOperation.developer.defGenTableColumn.isIncrement'),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          column: { width: 120 },
        },
        fill: {
          title: $t('devOperation.developer.defGenTableColumn.fill'),
          type: 'text',
          form: {
            component: {
              name: 'a-select',
              options: [
                { value: 'DEFAULT' },
                { value: 'INSERT' },
                { value: 'UPDATE' },
                { value: 'INSERT_UPDATE' },
              ],
            },
          },
        },
        isEdit: {
          title: $t('devOperation.developer.defGenTableColumn.isEdit'),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          column: { width: 120 },
        },
        isList: {
          title: $t('devOperation.developer.defGenTableColumn.isList'),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          column: { width: 120 },
        },
        isQuery: {
          title: $t('devOperation.developer.defGenTableColumn.isQuery'),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          column: { width: 120 },
        },
        width: {
          title: $t('devOperation.developer.defGenTableColumn.width'),
          type: 'string',
          column: { width: 120 },
        },
        queryType: {
          title: $t('devOperation.developer.defGenTableColumn.queryType'),
          type: 'text',
          form: {
            component: {
              name: 'a-auto-complete',
              options: [
                { value: 'EQUAL' },
                { value: 'NOT_EQUAL' },
                { value: 'LIKE' },
                { value: 'ORACLE_LIKE' },
                { value: 'LIKE_LEFT' },
                { value: 'LIKE_RIGHT' },
              ],
            },
          },
          column: { width: 120 },
        },
        component: {
          title: $t('devOperation.developer.defGenTableColumn.component'),
          type: 'text',
          form: {
            component: {
              name: 'a-auto-complete',
              options: [
                { value: 'Input' },
                { value: 'InputTextArea' },
                { value: 'InputPassword' },
                { value: 'InputNumber' },
                { value: 'AutoComplete' },
                { value: 'ApiAutoComplete' },
                { value: 'Select' },
                { value: 'ApiSelect' },
                { value: 'ApiTree' },
                { value: 'ApiTreeSelect' },
                { value: 'RadioButtonGroup' },
                { value: 'Switch' },
                { value: 'DatePicker' },
                { value: 'Upload' },
              ],
            },
          },
          column: { width: 120 },
        },
        vxeComponent: {
          title: $t('devOperation.developer.defGenTableColumn.vxeComponent'),
          type: 'text',
          form: {
            component: {
              name: 'a-auto-complete',
              options: [{ value: '$input' }, { value: '$select' }],
            },
          },
          column: { width: 120 },
        },
        soyComponent: {
          title: 'soybean组件',
          type: 'text',
          form: {
            component: {
              name: 'a-auto-complete',
              options: [
                { value: 'text' },
                { value: 'number' },
                { value: 'password' },
                { value: 'textarea' },
                { value: 'dict-select' },
                { value: 'table-select' },
                { value: 'dict-radio' },
                { value: 'dict-checkbox' },
                { value: 'dict-switch' },
                { value: 'datetime' },
                { value: 'date' },
                { value: 'time' },
                { value: 'daterange' },
                { value: 'datetimerange' },
                { value: 'file-uploader' },
                { value: 'image-uploader' },
                { value: 'avatar-uploader' },
                { value: 'avatar-cropper' },
              ],
            },
          },
          column: { width: 120 },
        },
        dictType: {
          title: $t('devOperation.developer.defGenTableColumn.dictType'),
          type: 'text',
          column: { width: 120 },
        },
        echoStr: {
          title: $t('devOperation.developer.defGenTableColumn.echoStr'),
          type: 'text',
          column: { width: 120 },
        },
        enumStr: {
          title: $t('devOperation.developer.defGenTableColumn.enumStr'),
          type: 'text',
          column: { width: 120 },
        },
        editDefValue: {
          title: $t('devOperation.developer.defGenTableColumn.editDefValue'),
          type: 'text',
          column: { width: 120 },
        },
        editHelpMessage: {
          title: $t('devOperation.developer.defGenTableColumn.editHelpMessage'),
          type: 'text',
          column: { width: 120 },
        },
        indexHelpMessage: {
          title: $t(
            'devOperation.developer.defGenTableColumn.indexHelpMessage',
          ),
          type: 'text',
          column: { width: 120 },
        },
        sortValue: {
          title: $t('devOperation.developer.defGenTableColumn.sortValue'),
          type: 'number',
          column: { width: 120 },
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = {
  addForm: {
    rules: {},
  },
  editForm: {
    rules: {},
  },
};
